package com.icez.frequencyplaying

import android.content.Context
import android.content.res.TypedArray
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.os.Handler
import android.os.Looper
import android.os.Message
import android.util.AttributeSet
import android.util.Log
import android.view.View
import androidx.core.view.postDelayed
import java.util.*

class FrequencyPlayingView @JvmOverloads constructor(
    context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : View(context, attrs, defStyleAttr) {
    // 画笔
    private var paint: Paint? = null
    // 画笔颜色
    private var mLineColor:Int = Color.RED
    // 线的宽度
    private var mLineWidth = 10f
    // 线的数量
    private var mLineNum = 3
    // 线的间距
    private var mLineSpace = 0f

    // 是否开始
    private var isStart:Boolean = false

    private var isAutoStart:Boolean = false

    // 所有线的高度位置
    var mLineTopList:ArrayList<Float> = arrayListOf()

    init {
        paint = Paint(Paint.ANTI_ALIAS_FLAG)

        val typeArray = context.obtainStyledAttributes(attrs,R.styleable.FrequencyPlayingView)
        mLineColor = typeArray.getColor(R.styleable.FrequencyPlayingView_line_color,Color.RED)
        mLineWidth = typeArray.getDimension(R.styleable.FrequencyPlayingView_line_width,10f)
        mLineNum = typeArray.getInteger(R.styleable.FrequencyPlayingView_line_num,3)
        isAutoStart = typeArray.getBoolean(R.styleable.FrequencyPlayingView_is_autostart,true)
        typeArray.recycle()

        paint?.color = mLineColor
    }

    override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {
        super.onLayout(changed, left, top, right, bottom)
        for(index in 0..mLineNum){
            mLineTopList.add(height.toFloat())
        }
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)
        // 获取线的间距
        mLineSpace = width / mLineNum.toFloat() - mLineWidth

        // 设置线的x初始位置
        var mLineSpaceX = mLineSpace / 2f
        // 绘制每一个线条
        for (index in 0..mLineNum) {
            paint?.let {
                canvas?.drawRoundRect(
                    mLineSpaceX,
                    mLineTopList.get(index),
                    mLineSpaceX + mLineWidth,
                    height.toFloat(),
                    mLineWidth,
                    mLineWidth,
                    it
                )
            }
            mLineSpaceX = mLineSpaceX + mLineWidth + mLineSpace
        }
        if(isAutoStart){
            start()
        }

    }

    /**
     * @sample 开始动画
     */
    public fun start() {
        if(isStart){
            return
        }
        isStart = true
        Thread {
            for (index in 0..Int.MAX_VALUE) {
                if(!isStart){
                    break
                }
                for (index in 0..mLineNum) {
                    val r = Math.random()
                    mLineTopList.add(index,(height-mLineWidth)*r.toFloat())
                }
                Thread.sleep(200)
                post {
                    invalidate()
                }
            }
        }.start()
    }

    /**
     * @sample 停止动画
     */
    public fun stop(){
        isAutoStart = false
        isStart = false
    }

}